package com.lxy.controller;

import com.lxy.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Controller
@EnableDiscoveryClient
@EnableCircuitBreaker
//@DefaultProperties(defaultFallback = "fallBack")
@EnableFeignClients
public class UserController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;
    @GetMapping("/user/{id}")
    @ResponseBody
    @HystrixCommand
    public User a(@PathVariable("id") String id){
        String url = "http://PROVIDER-SERVER/to/"+id;
        System.out.println(url);
        return this.restTemplate.getForObject(url,User.class);

    }
    public User fallBack(){
        return new User("404","页面不存在","","","");
    }
    public User fallBack(String id){
        return new User("500","服务器繁忙，请稍后在试","20","10","T");
    }
}
